package cn.edu.zafu.bigdata.lhff;

import java.io.*;
import java.net.*;

public class TCPClient {
    public static void main(String[] args) {
        String serverAddress = "127.0.0.1";  // 服务端地址
        int port = 12345;  // 服务端端口

        try (Socket socket = new Socket(serverAddress, port);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in))) {

            System.out.println("Connected to server at " + serverAddress + ":" + port);

            String message;
            while (true) {
                System.out.print("Enter message (or 'exit' to quit): ");
                message = userInput.readLine();

                if ("exit".equalsIgnoreCase(message)) {
                    break;
                }

                // 发送消息到服务端
                out.println(message);

                // 接收服务端的回复
                String response = in.readLine();
                System.out.println("Server reply: " + response);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
